package com.example.myweather.adapter

import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.example.myweather.R
import com.example.myweather.pojo.Forecast
import kotlinx.android.synthetic.main.item_forecast.view.*
import org.jetbrains.anko.find

class ForecastAdapter(val items: List<Forecast>?,val itemClick:(Int) -> Unit) : RecyclerView.Adapter<ForecastAdapter.ViewHolder>() {
    var mListener: ((Int) -> Unit?)? = null

    fun setOnItemClickListener(listener: (Int) -> Unit) {
        mListener = listener
    }
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
        var rootView = View.inflate(p0.context, R.layout.item_forecast, null)
        return ViewHolder(rootView)
    }

    override fun getItemCount(): Int {
        return items?.size!!
    }

    override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
        /*with(p0){
            var cur = items?.get(p1)
            text.text = cur!!.date+"日   "+cur.high+cur.low+"     "+cur.type
           itemView.setOnClickListener { mListener?.invoke(p0.layoutPosition) }
        }*/

        with(this!!.items!![p1]) {
            p0.root.tv_info.text = "$date 号       $high - $low       $type"
//            p0.itemView.setOnClickListener { mListener?.invoke(p0.layoutPosition) }
            p0.itemView.setOnClickListener { itemClick(p0.layoutPosition) }
        }
    }

    class ViewHolder(val root: View) : RecyclerView.ViewHolder(root) {
        private val tv_info:TextView = root.find(R.id.tv_info)
        private val iv:ImageView = root.find(R.id.iv)
    }

}